
 
package com.magicroot.gui.crudf;

import nextapp.echo.app.Extent;
import nextapp.echo.app.TextField;

import com.magicroot.model.MCrudF;
import com.magicroot.model._PropMCrudF;
import com.minotauro.echo.beans.EFieldLabel;
import com.minotauro.echo.cleda.edit.FrmEditBase;
import com.minotauro.echo.grid.FieldModel;
import com.minotauro.echo.grid.SectionModel;
import com.minotauro.echo.grid.TabModel;
import com.minotauro.echo.validator.impl.DuplicatedValidator;
import com.minotauro.echo.validator.impl.NotEmptyValidator;



public class FrmMCrudFEdit extends FrmEditBase {

	private FieldModel fmName;
	private FieldModel fmDesc;

	// --------------------------------------------------------------------------------

	public FrmMCrudFEdit() {
		// Empty
	}

	// --------------------------------------------------------------------------------

	@Override
	protected void initGUI() {
		String name = ((MCrudF) data).getName();
		setTitle(_I18NFrmMCrudFEdit.title(name == null ? "-" : name));

		// --------------------------------------------------------------------------------

		formModel.setShowTabs(true);

		initTabA();
		initTabB();
		// initTab("B", 5);
		// initTab("C", 5);
		// initTab("D", 5);
		// initTab("E", 5);
	}

	// --------------------------------------------------------------------------------

	protected void initTabA() {
		TabModel tabModel = new TabModel();
		tabModel.setLabelCmp(new EFieldLabel(_I18NFrmMCrudFEdit.tab("A"), null));
		formModel.addChild(tabModel);

		SectionModel sectionModel = new SectionModel();
		tabModel.addChild(sectionModel);

		// --------------------------------------------------------------------------------

		TextField txtName = new TextField();
		txtName.setWidth(new Extent(204));

		fmName = new FieldModel();
		fmName.setLabelCmp(new EFieldLabel(_I18NFrmMCrudFEdit.name()));
		fmName.setFieldCmp(txtName);
		fmName.setKey(_PropMCrudF.NAME);
		fmName.setProperty(_PropMCrudF.NAME);

		fmName.getValidatorList().add( //
				new NotEmptyValidator(_I18NFrmMCrudFEdit.name(), txtName));

		sectionModel.addChild(fmName);

		fmName.getValidatorList().add( //
				new NotEmptyValidator(_I18NFrmMCrudFEdit.name(), txtName));

		sectionModel.addChild(fmName);

		// --------------------------------------------------------------------------------

		DuplicatedValidator duplicatedValidator = new DuplicatedValidator(data);
		duplicatedValidator.add(fmName);
		formModel.getValidatorList().add(duplicatedValidator);
	}

	// --------------------------------------------------------------------------------
	protected void initTabB() {
		TabModel tabModel = new TabModel();
		tabModel.setLabelCmp(new EFieldLabel(_I18NFrmMCrudFEdit.tab("B"), null));
		formModel.addChild(tabModel);

		SectionModel sectionModel = new SectionModel();
		tabModel.addChild(sectionModel);

		// --------------------------------------------------------------------------------

		TextField txtName = new TextField();
		txtName.setWidth(new Extent(204));

		fmName = new FieldModel();
		fmName.setLabelCmp(new EFieldLabel(_I18NFrmMCrudFEdit.numero()));
		fmName.setFieldCmp(txtName);
		fmName.setKey(_PropMCrudF.NUMERO);
		fmName.setProperty(_PropMCrudF.NUMERO);

		fmName.getValidatorList().add( //
				new NotEmptyValidator(_I18NFrmMCrudFEdit.name(), txtName));

		sectionModel.addChild(fmName);

		fmName.getValidatorList().add( //
				new NotEmptyValidator(_I18NFrmMCrudFEdit.name(), txtName));

		sectionModel.addChild(fmName);

		// --------------------------------------------------------------------------------

		DuplicatedValidator duplicatedValidator = new DuplicatedValidator(data);
		duplicatedValidator.add(fmName);
		formModel.getValidatorList().add(duplicatedValidator);
	}

	protected void initTab(String prefix, int count) {
		TabModel tabModel = new TabModel();
		tabModel.setLabelCmp(new EFieldLabel(_I18NFrmMCrudFEdit.tab(prefix),
				null));
		formModel.addChild(tabModel);

		SectionModel sectionModel = new SectionModel();
		tabModel.addChild(sectionModel);

		// --------------------------------------------------------------------------------

		for (int i = 1; i <= count; i++) {
			String key = "val" + prefix + i;
			String lbl = _I18NFrmMCrudFEdit.lbl(prefix, i);
			String val = _I18NFrmMCrudFEdit.val(prefix, i);

			TextField txtVal = new TextField();
			FieldModel fmVal = new FieldModel();
			fmVal.setLabelCmp(new EFieldLabel(lbl));
			fmVal.setFieldCmp(txtVal);
			fmVal.setKey(key);
			fmVal.setProperty(key);

			fmVal.getValidatorList().add(new NotEmptyValidator(val, txtVal));

			sectionModel.addChild(fmVal);
		}
	}
}
